/**  
*  Doku2Buddy - A DokuWiki to BuddyPress Wiki-Component Converter
*  Copyright (C) 2010 Skye Book
*  
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*  
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*  
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.sbook.d2b.formatting;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;

import com.sbook.d2b.db.DBManager;

/**
 * @author Skye Book
 *
 */
public class BuddyPressFormatter {
	private CodeConverter codeConverter;
	private ArrayList<BuddyPressArticle> articles = new ArrayList<BuddyPressArticle>();
	private DBManager dbMan;

	/**
	 * 
	 */
	public BuddyPressFormatter(ArrayList<DokuArticle> dokuArticles, DBManager dbMan, String fallbackUser, String bpGroup) {
		this.dbMan=dbMan;
		Iterator<DokuArticle> it = dokuArticles.iterator();
		
		codeConverter = new SyntaxEditorCodeConverter();
		
		while(it.hasNext()){
			DokuArticle dokuArticle = it.next();
			BuddyPressArticle article = new BuddyPressArticle();
			
			article.setTitle(dokuArticle.getTitle());
			
			
			article = configureUser(article, dokuArticle.getAuthor(), "email", fallbackUser);
			
			
			
			//article.setMarkup(dokuArticle.getMarkup());
			article.setMarkup(codeConverter.convertCode(dokuArticle));
			
			article.setSqlDate(convertDate(dokuArticle.getDate()));
			
			article.setArticleSlug(createSlug(article.getTitle()));
			
			article.setGroupName(bpGroup);
			
			int groupID = dbMan.findGroupIDByName(article.getGroupName());
			if(groupID!=0){
				article.setGroupID(groupID);
			}
			
			article = createPostName(article);
			
			articles.add(article);
		}
	}
	
	private BuddyPressArticle configureUser(BuddyPressArticle article, String dokuAuthor, String dokuEmail, String fallbackUser){
		int userSearchResult = dbMan.findUserViaName(dokuAuthor);
		if(userSearchResult==-1){
			int emailSearchResult = dbMan.findUserViaName(dokuEmail);
			if(emailSearchResult==-1){
				// use default user
				int idResponse = dbMan.findUserViaName(fallbackUser);
				if(idResponse>0){
					article.setAuthor(fallbackUser);
					article.setUserID(idResponse);
				}
				else{
					// ERROR
				}
			}
			else if(emailSearchResult==-2){
				// SQL error
			}
			else{
				article.setUserID(emailSearchResult);
				String authorSearchResult = dbMan.findUserByID(emailSearchResult);
				if(authorSearchResult != "NOT_FOUND" && authorSearchResult != "SQL_ERROR"){
					article.setAuthor(authorSearchResult);
				}
				else{
					// there is a problem.
				}
			}
		}
		else if(userSearchResult==-2){
			// SQL error
		}
		else{
			article.setAuthor(dokuAuthor);
			article.setUserID(userSearchResult);
		}
		return article;
	}
	
	private BuddyPressArticle createPostName(BuddyPressArticle article){
		if(article.getGroupID() != -1){
			String articleName = ""+article.getGroupID()+"-";
			articleName+=createSlug(article.getTitle());
			article.setPostName(articleName);
		}
		return article;
	}
	
	private String createSlug(String originalName){
		return originalName.replaceAll("\'", "").replaceAll(" ", "-").replace("!", "").toLowerCase();
	}
	
	private Date convertDate(long dokuSeconds){
		return new Date(dokuSeconds*1000);
	}

	public ArrayList<BuddyPressArticle> getArticles() {
		return articles;
	}
}
